<!-- 产品分类管理 -->
<template>
  <ContainerSlot>
    <template slot="searchModule">
      <SearchForm :labelWidth="'120px'" :formConfig="formConfig" :formData="formData" :slotList="slotList"
        ref="ruleForm">
        <template slot="searchBtn">
          <el-button type="primary" @click.prevent="search()">查询</el-button>
        </template>
        <template slot="resetBtn">
          <el-button @click.prevent="resetForm()">重置</el-button>
        </template>
      </SearchForm>
    </template>
    <template slot="operationModule">
      <el-button class="optBtn" type="primary" plain @click="add">
        <i class="el-icon-circle-plus-outline"></i>
        添加
      </el-button>
      <el-button class="optBtn" type="danger" plain @click="handleDelete()" :disabled="!delActive">
        <i class="el-icon-delete"></i>
        删除
      </el-button>
    </template>
    <template slot="contentModule">
      <Table v-loading="loading" :propList="propList" :listData="tableData" :showSelectColumn="true"
        @handleSelectionChange="handleSelectionChange">
        <template slot="operation" slot-scope="scope" v-if="scope.row.productTypeId">
          <el-button class="optBtn" type="primary" @click="handleEdit(scope.row)" plain>
            <i class="el-icon-edit-outline"></i>
            修改
          </el-button>
          <el-button class="optBtn" type="danger" @click="handleDelete(scope.row)" plain>
            <i class="el-icon-delete"></i>
            删除
          </el-button>
        </template>
      </Table>
      
      <el-dialog :close-on-click-modal='false'
        :title="dialogTitle+'产品分类'"
        :visible.sync="dialogFormVisible"
        :before-close="cancel"
        width="30%"
      >
        <el-form
          ref="addForm"
          label-position="right"
          :model="addForm"
          class="el-form"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="产品分类名称:" prop="productTypeName">
            <el-input
              v-model="addForm.productTypeName"
              clearable
            />
          </el-form-item>
          <el-form-item label="备注:">
            <el-input
              v-model="addForm.remarks"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4 }"
              clearable
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel">
            取 消
          </el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="save"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
    </template>
    <template slot="footerModule">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        background :page-size="pageSize" :page-sizes="[10, 20, 30]" layout=" prev, pager, next, jumper" :total="total">
      </el-pagination>
    </template>
  </ContainerSlot>
</template>

<script>
import ContainerSlot from '@/components/Container'
import SearchForm from '@/components/Form'
import Table from '@/components/Table'

import productTypeApi from "@/api/breed-aquatics-popular/product-library/product-sort-management"

export default {
  name: '',
  components: { ContainerSlot, SearchForm, Table },
  data () {
    return {
      delActive: false,
      loading: false,
      // 表单配置
      formConfig: [
        {
          label: '产品分类名称',
          prop: 'productTypeName',
          type: 'input',
          width: '150px'
        }
      ],
      //插槽
      slotList: [{
        slotName: 'searchBtn'
      },
      {
        slotName: 'resetBtn'
      }],
      // 表单数据
      formData: {
        productTypeName: '',
      },
      total: 2,
      currentPage: 1,
      pageSize: 10,
      propList: [
        { prop: "productTypeName", label: "产品分类名称" },
        { prop: "remarks", label: "备注" },
        { label: "操作", slotName: "operation" },
      ],
      tableData: [],
      // 新增编辑弹框
      dialogTitle: '',
      dialogFormVisible: false,
      nowChanceStau: '',
      addForm: {
        productTypeName: '',
        remarks: '',
      },
      rules: {
        productTypeName: [
          { required: true, message: '请输入批次名称', trigger: ['change', 'blur'] },
        ],
      }
    }
  },
  watch: {
    dialogFormVisible(v) {
      if (v) {
        this.$nextTick(()=>{
          this.$refs.addForm.clearValidate()
        })
      }
    }
  },
  mounted() {
    this.queryPageList()
  },
  methods: {
    queryPageList () {
      this.loading = true
      productTypeApi.queryPageByCondition({
        page: this.currentPage,
        size: this.pageSize,
        condition: {
          productTypeName: this.formData.productTypeName || ''
        }
      }).then(res => {
        let { data } = res
        this.tableData = data.content
        this.total = data.totalElements
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 搜索
    search() {
      this.currentPage = 1
      this.queryPageList()
    },
    resetForm () {
      this.$refs.ruleForm.resetForm()
      this.search()
    },
    handleCurrentChange (e) {
      this.currentPage = e
      this.queryPageList()
    },
    handleSizeChange (e) {
      this.pageSize = e
      this.queryPageList()
    },
    add() {
      this.openDialog('add')
    },
    handleEdit(row) {
      this.addForm = { ...row }
      this.openDialog('edit')
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      }).then(() => {
        productTypeApi.delList({
          idList: (row ? [row.productTypeId] : this.idList).join(',')
        }).then(res => {
          if (res.code == 200) {
            this.$message.success("删除成功！")
            this.queryPageList()
          } else {
            this.$message.error(res.msg)
          }
        })
      })
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === "add" ? "新增" : "编辑";
      this.nowChanceStau = type;
    },
    cancel() {
      this.dialogFormVisible = false;
      this.addForm = {};
    },
    save() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          productTypeApi[this.nowChanceStau](this.addForm).then(res => {
            if (res.code == 200) {
              this.$message.success(res.msg)
              this.cancel()
              this.queryPageList()
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    handleSelectionChange(list) {
      this.idList = [];
      this.idList = list.map(item => {
        return item.productTypeId;
      });
      if (
        this.idList &&
        this.idList.length > 0
      ) {
        this.delActive = true;
      } else this.delActive = false;
    },
  }
}
</script>

<style lang="scss" scoped>
  ::v-deep .el-dialog__header {
    width: 100%;
    background-color: #329eef;
    padding: 15px;
    .el-dialog__title {
      font-weight: 700;
      color: #fff;
    }
    .el-dialog__close {
      font-size: 22px;
      color: #fff;
      font-weight: 700;
    }
  }
  .el-form-item {
    // display: flex;
    .el-form-item__label {
      line-height: 36px;
      text-align: end;
      font-weight: normal;
    }
    .el-form-item__content {
      line-height: 36px;
      width: 100%;
      display: flex;
    }
  }
</style>
